/*
 *    Copyright 2008 Lammert Westerhoff
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.maven.plugins.dependencyconflicts;

import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.tree.DependencyNode;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;

/**
 * Resolves dependency conflicts and can stop a build if it contains dependency conflicts. The info, warning and error
 * conflicts are logged.
 * 
 * @author Lammert Westerhoff
 * @execute phase="compile"
 * @goal analyze
 */
public class DependencyConflictsAnalyzeMojo
    extends AbstractMojo
{

    /**
     * @component
     */
    private DependencyConflictsResolver dependencyConflictsResolver;

    /**
     * @component
     */
    private DependencyTreeBuilder dependencyTreeBuilder;

    /**
     * @component
     */
    private ArtifactCollector artifactCollector;

    /**
     * @component
     */
    private ArtifactMetadataSource artifactMetaSource;

    /**
     * @component
     */
    private ArtifactFactory artifactFactory;

    /**
     * The local repository that is used by the project.
     * 
     * @parameter expression="${localRepository}
     */
    private ArtifactRepository localRepository;

    /**
     * @parameter expression="${project}
     * @readonly
     */
    private MavenProject mavenProject;

    /**
     * Determines whether or not to abort the build when encountering an error dependency conflict.
     * 
     * @parameter default="false"
     */
    private boolean failOnError;

    /**
     * Determines whether or not to abort the build when encountering a warning dependency conflict.
     * 
     * @parameter default="false"
     */
    private boolean failOnWarning;

    /*
     * (non-Javadoc)
     * @see org.apache.maven.plugin.Mojo#execute()
     */
    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        try
        {
            final DependencyNode rootNode =
                getDependencyTreeBuilder().buildDependencyTree( getMavenProject(), getLocalRepository(),
                                                                getArtifactFactory(), getArtifactMetaSource(), null,
                                                                getArtifactCollector() );
            DependencyConflicts dependencyConflicts = getDependencyConflictsResolver().resolve( rootNode );

            for ( final DependencyNode node : dependencyConflicts.getErrorConflicts() )
            {
                getLog().error( node.toNodeString() );
            }
            for ( final DependencyNode node : dependencyConflicts.getWarningConflicts() )
            {
                getLog().warn( node.toNodeString() );
            }
            for ( final DependencyNode node : dependencyConflicts.getInfoConflicts() )
            {
                getLog().info( node.toNodeString() );
            }
            if ( isFailOnError() && dependencyConflicts.getErrorConflicts().size() > 0 )
            {
                throw new MojoExecutionException( "Dependency conflicts found." );
            }
            if ( isFailOnWarning() && dependencyConflicts.getWarningConflicts().size() > 0 )
            {
                throw new MojoExecutionException( "Dependency conflicts found." );
            }
        }
        catch ( final DependencyTreeBuilderException e )
        {
            throw new MojoFailureException( e.getMessage(), e );
        }
    }

    /**
     * @return the mavenProject
     */
    public MavenProject getMavenProject()
    {
        return this.mavenProject;
    }

    /**
     * @return the localRepository
     */
    public ArtifactRepository getLocalRepository()
    {
        return this.localRepository;
    }

    /**
     * @return the dependencyTreeBuilder
     */
    public DependencyTreeBuilder getDependencyTreeBuilder()
    {
        return this.dependencyTreeBuilder;
    }

    /**
     * @return the artifactCollector
     */
    public ArtifactCollector getArtifactCollector()
    {
        return this.artifactCollector;
    }

    /**
     * @return the artifactMetaSource
     */
    public ArtifactMetadataSource getArtifactMetaSource()
    {
        return this.artifactMetaSource;
    }

    /**
     * @return the artifactFactory
     */
    public ArtifactFactory getArtifactFactory()
    {
        return this.artifactFactory;
    }

    /**
     * @return the dependencyConflictsResolver
     */
    public DependencyConflictsResolver getDependencyConflictsResolver()
    {
        return this.dependencyConflictsResolver;
    }

    /**
     * @return the failOnError
     */
    protected final boolean isFailOnError()
    {
        return this.failOnError;
    }

    /**
     * @return the failOnWarning
     */
    protected final boolean isFailOnWarning()
    {
        return this.failOnWarning;
    }
}
